﻿using System;
using System.IO;
using System.Xml.Serialization;
using MobySharp.Results;

namespace MobySharp.Actions
{
    public class GetComments : BaseAction, IAction
    {
        private readonly long _postId;
        private readonly string _tinyURLCode;

        public GetComments(long postId)
        {
            _postId = postId;
            RequestType = HTTPRequestType.Get;
            Action = APICall.getComments;
        }

        public GetComments(string tinyURLCode)
        {
            _tinyURLCode = tinyURLCode;
            RequestType = HTTPRequestType.Get;
            Action = APICall.getComments;
        }

        [PostDataInstruction("post_id", false)]
        public long PostId
        {
            get { return _postId; }
        }

        [PostDataInstruction("tinyurl_code", false)]
        public string TinyURLCode
        {
            get { return _tinyURLCode; }
        }

        #region IAction Members

        IResult IAction.ParseResult(string result)
        {
            CommentsResult m = null;
            try
            {
                if (result.Contains("<?xml"))
                {
                    var serializer = new XmlSerializer(typeof (CommentsResult));
                    m = (CommentsResult) serializer.Deserialize(new StringReader(result));

                    return m;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return m;
        }

        #endregion
    }
}